﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;

namespace DAGG.Ashx
{
    /// <summary>
    /// uploadNew 的摘要说明
    /// </summary>
    public class uploadNew : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {

            context.Response.ContentType = "text/html";
            if (context.Request.Files.Count > 0)
            {
                HttpPostedFile file1 = context.Request.Files["myfile"];
                string action = context.Request["oper"];
                  string savepath="";
                if (action=="add")
                      savepath = uploadImg(file1, "~/upload/", action);  //这里引用的是上面封装的方法
              if (action=="temp")
                      savepath = uploadImg(file1, "~/TempImages/", action);  //这里引用的是上面封装的方法
           
               WriteJsonNew(context.Response, savepath);
            }
            else
            {
              //  WriteJsonNew(context.Response, "请选择要上传的文件");
            }
        }
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="file">通过form表达提交的文件</param>
        /// <param name="virpath">文件要保存的虚拟路径</param>
        public static string uploadImg(HttpPostedFile file, string virpath, string action)
        {
            string savepath = string.Empty;
            if (file.ContentLength > 1024 * 1024 * 4)
            {
                throw new Exception("文件不能大于4M");
            }
            string imgtype = Path.GetExtension(file.FileName);
            if (imgtype != ".jpg" && imgtype != ".jpeg")  //图片类型进行限制
            {
                throw new Exception("请上传jpg或JPEG图片");
            }
            using (Image img = Bitmap.FromStream(file.InputStream))
            {
                savepath = HttpContext.Current.Server.MapPath(virpath + file.FileName);
             
                if (action == "add")//保存
                {
                   
                    //清空临时文件夹
                    foreach (string d in Directory.GetFileSystemEntries(HttpContext.Current.Server.MapPath("~/TempImages")))
                    {
                        if (File.Exists(d))
                        {
                            FileInfo fi = new FileInfo(d);
                            if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1)
                                fi.Attributes = FileAttributes.Normal;
                            File.Delete(d);//直接删除其中的文件  
                        }
                    }
                }
                img.Save(savepath);

                savepath = " http://danganguan.m12315.com/" + virpath.Substring(2, virpath.Length-2) + file.FileName;
             
            }
            return savepath;
        }
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="file">通过form表达提交的文件</param>
        /// <param name="virpath">文件要保存的虚拟路径</param>
        public static void uploadFile(HttpPostedFile file, string virpath,string action )
        {
            if (file.ContentLength > 1024 * 1024 * 6)
            {
                throw new Exception("文件不能大于6M");
            }
            string imgtype = Path.GetExtension(file.FileName);
            //imgtype对上传的文件进行限制
            if (imgtype != ".zip" && imgtype != ".mp3")
            {
                throw new Exception("只允许上传zip、rar....文件");
            }
            string dirFullPath = HttpContext.Current.Server.MapPath(virpath);
            if (!Directory.Exists(dirFullPath))//如果文件夹不存在，则先创建文件夹
            {
                Directory.CreateDirectory(dirFullPath);
            }

            file.SaveAs(dirFullPath + file.FileName);

        }


        public static void WriteJsonNew(HttpResponse response,
           string savePath)
        {
            response.ContentType = "application/json";
           
            var obj = new { saPath = savePath};
            string json = new JavaScriptSerializer().Serialize(obj);
            response.Write(json);
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}